﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace V32VM
{
    public partial class MainForm : Form
    {
        public static List<IMemoryUpdatable> ViewForms { get; set; }

        public MainForm()
        {
            InitializeComponent();

            ViewForms = new List<IMemoryUpdatable>();
            displayRegisters();

            Watch.OnWatchesChanged += watchesChanged;
        }

        private void UpdateMemory()
        {
            foreach (IMemoryUpdatable updatable in ViewForms)
                updatable.UpdateMemory();
        }

        private void step()
        {
            Program.VM.step();
            UpdateMemory();
            displayRegisters();
        }

        private void displayRegisters()
        {
            regALabel.Text = "A: " + Program.VM.regs.a;
            regBLabel.Text = "B: " + Program.VM.regs.b;
            regCLabel.Text = "C: " + Program.VM.regs.c;
            regDLabel.Text = "D: " + Program.VM.regs.d;
            regELabel.Text = "E: " + Program.VM.regs.e;
            regFLabel.Text = "F: " + Program.VM.regs.f;
            regGLabel.Text = "G: " + Program.VM.regs.g;
            regTLabel.Text = "T: " + Program.VM.regs.t;
            regULabel.Text = "U: " + Program.VM.regs.u;
            regVLabel.Text = "V: " + Program.VM.regs.v;
            regWLabel.Text = "W: " + Program.VM.regs.w;
            regXLabel.Text = "X: " + Program.VM.regs.x;
            regYLabel.Text = "Y: " + Program.VM.regs.y;
            regZLabel.Text = "Z: " + Program.VM.regs.z;
            regSLabel.Text = "S: " + Program.VM.regs.s;
            regIPLabel.Text = "IP: " + Program.VM.regs.ip;
            regSPLabel.Text = "SP: " + Program.VM.regs.sp;
            regFLLabel.Text = "FL: " + Program.VM.regs.fl;
        }

        private void addMemoryView()
        {
            MemoryViewForm memoryView = new MemoryViewForm(new ConstantSource(0));
            memoryView.Show(this);
            ViewForms.Add(memoryView);
        }
        private void addDisassemblyView()
        {
            DisassemblerViewForm disassemblyView = new DisassemblerViewForm(new ConstantSource(0));
            disassemblyView.Show(this);
            ViewForms.Add(disassemblyView);
        }
        private void addWatchView()
        {
            WatchForm watchView = new WatchForm();
            watchView.Show(this);
            ViewForms.Add(watchView);
        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case 's': step(); break;
                case 'm': addMemoryView(); break;
                case 'd': addDisassemblyView(); break;
                case 'w': addWatchView(); break;
            }
        }

        private void watchesChanged()
        {
            UpdateMemory();
        }
    }
}
